package com.lagou.task.servlet;

import com.lagou.task.entity.Student;
import com.lagou.task.entity.SysClass;
import com.lagou.task.service.ClassService;
import com.lagou.task.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ClassFindServlet",urlPatterns = "/classFind")
public class ClassFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("UTF-8");
        String className = request.getParameter("selectByName");
        if (className != null && !className.equals("")){
            ClassService classService = new ClassService();
            SysClass findClass = classService.findClassService(className);
            if (findClass != null){
                request.getSession().setAttribute("findClass",findClass);
                response.sendRedirect("classFind.jsp");
            }else {
                request.setAttribute("findError","findError");
                // 实现服务器跳转  共享request和response对象
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("class");
                requestDispatcher.forward(request, response);
            }
        }else {
            //response.sendRedirect("list");
            request.setAttribute("findNull","findNull");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("class");
            requestDispatcher.forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
